package com.example.ecmftlutil;

import org.apache.tomcat.util.buf.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

public class DataPrint2 {

    public static void print(Test.Node node, List<String> onlineCode) {
        if (null == node) {
            return;
        }
        List<String> result = new ArrayList<>();
        if (node.getChildren().size() == 0) {
            setDataFilter(node, result, onlineCode);
            output(node, onlineCode, result);
            return;
        }
        getLeafNodes(node, result, onlineCode);
//        if (!CollectionUtils.isEmpty(result)) {
        //输出结果
        output(node, onlineCode, result);
//        System.out.println(node.getCode() + ":" + result);
//        }
        for (Test.Node child : node.getChildren()) {
            print(child, onlineCode);
        }
    }

    private static void output(Test.Node node, List<String> onlineCode, List<String> result) {
        String code = node.getCode();
        if (onlineCode.contains(code)) {
//            System.out.println(code + ":" + result.stream().filter(str -> onlineCode.contains(str)).collect(Collectors.toList()));
            System.out.println(code + ":" + StringUtils.join(result.stream().filter(str -> onlineCode.contains(str)).collect(Collectors.toList())));
        }
    }

    private static void getLeafNodes(Test.Node node, List<String> result, List<String> onlineCode) {
        if (null == node) {
            return;
        }
        setDataFilter(node, result, onlineCode);
        if (node.getChildren().size() == 0) {
            return;
        }
        for (Test.Node child : node.getChildren()) {
            getLeafNodes(child, result, onlineCode);
        }
    }

    private static void setDataFilter(Test.Node node, List<String> result, List<String> onlineCode) {
        // 上线数据需要输出
//        if (onlineCode.contains(node.getCode()) && onlineCode.contains(node.getParentCode()) ) {
//        if (onlineCode.contains(node.getCode())) {
        result.add(node.getCode());
//        }
    }
}
